package com.core.domain;

import com.core.enums.ResultCode;
import lombok.Data;

@Data
public class Result <T>{
    private Integer code;
    private String msg;
    private T data;

    /**
     * 成功(不需要返回数据)
     * @return
     * @param <T>
     */
    public static <T> Result<T> ok(){
       return assembleResult(null,ResultCode.SUCCESS);
    }

    /**
     * 成功(需要返回数据)
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> ok(T data){
        return assembleResult(data,ResultCode.SUCCESS);
    }

    /**
     * 直接返回操作失败
     * @return
     * @param <T>
     */

    public static <T> Result<T> fail(){
        return assembleResult(null,ResultCode.FAILED);
    }

    /**
     * 指定返回什么错误
     * @param resultCode
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(ResultCode resultCode){
        return assembleResult(null,resultCode);
    }



    public static <T> Result<T> fail(int code, String msg) {
        return assembleResult(code, msg, null);
    }

    private static <T> Result<T> assembleResult(int code, String msg, T data) {
        Result<T> r = new Result<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }



        /**
         * 根类
         * @param data
         * @param resultCode
         * @return
         * @param <T>
         */
    private static <T> Result<T> assembleResult(T data, ResultCode resultCode){
        Result<T> result=new Result<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return result;
    }
}
